﻿--lua script
--处理盗梦空间相关的操作

--#include "data\config\diamond\DiamondConfig.lua" once --宝石常规配置
--#include "data\functions\Activity\Common\Consumes.lua" once

DiamondOpId = {
	enUpgrade = 1,		--宝石升级
	enSmelt = 2,		--宝石熔炼
	enDestroy = 3,		--宝石分解
	enOneKey = 4,		--一键分解
}

DiamondOpFuncList = {}

EventCallDispatcher.HandlerDiamondOp = function (sysarg, args)
	if args[1] == nil then return end   --
	local func = DiamondOpFuncList[args[1]]
	if func ~= nil then
		func(sysarg, args) 
	end
end

--升级宝石
DiamondOpFuncList[DiamondOpId.enUpgrade] = function (sysarg,args)
	local nOwnerType = args[2]	--所属实体类型
	local nEquipPos = args[3]	--装备槽
	local nSlotIdx = args[4]	--宝石槽
	local wItemId = args[5]		--当前宝石ID
	local nHeroPos = args[6]	--英雄pos
	local nOpType = args[7]	--操作选择

	if wItemId <= 0 then
		return
	end
	
	local config = DiamondConfig
	
	if not config then return end

	local itemIndex = Item.getItemPropertyById(wItemId,Item.ipItemStaticDuaMax)

	local itemLevel = Item.getItemPropertyById(wItemId,Item.ipItemSuitId)

	local diamonName =  config.diamondName[itemIndex] or ""

	if nSlotIdx + 1 ~= itemIndex then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.SoulDiamond003, ttFlyTip)
		return
	end

	if not config.upgradeConsumes or not config.upgradeConsumes[itemIndex] or not config.upgradeConsumes[itemIndex][itemLevel] then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.SoulDiamond010, ttFlyTip)
		return
	end
	
	local consumes = config.upgradeConsumes[itemIndex][itemLevel]

	if nOpType == 0 then
		if not consumes.upGradeConsumes or #consumes.upGradeConsumes == 0 then
			Actor.sendTipmsg(sysarg, Lang.ScriptTips.SoulDiamond010, ttFlyTip)
			return
		end
	elseif nOpType == 1 then
		if not consumes.upGradeGoldConsumes or #consumes.upGradeGoldConsumes == 0 then
			Actor.sendTipmsg(sysarg, Lang.ScriptTips.SoulDiamond010, ttFlyTip)
			return
		end
	else
		return
	end

	local newItemId = consumes.upItemId

	local itemNewLevel = Item.getItemPropertyById(newItemId,Item.ipItemSuitId)

	local itemNewIndex = Item.getItemPropertyById(newItemId,Item.ipItemStaticDuaMax)

	if itemNewLevel ~= itemLevel + 1 then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.SoulDiamond004, ttFlyTip)
		return
	end

	if itemNewIndex ~= itemIndex then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.SoulDiamond005, ttFlyTip)
		return
	end

	local condition = config.upgradeConditions[itemNewIndex][itemNewLevel]
	
	if condition and condition.holeNum and condition.holeLevel and condition.holeNum > 0 and condition.holeLevel > 0 and 
		not Actor.checkDiamondCondition(sysarg, nOwnerType, nEquipPos, condition.holeLevel, condition.holeNum) then
		Actor.sendTipmsg(sysarg, string.format(Lang.ScriptTips.SoulDiamond002, condition.holeNum, condition.holeLevel), ttFlyTip)
		return
	end

	if nOpType == 0 then
		if not CheckConsumeCond( sysarg, consumes.upGradeConsumes, 1 ) then
			return false
		end
		
		if not DoConsumeCond( sysarg, consumes.upGradeConsumes, 1, 174, Lang.LogTips.l00095) then
			return
		end
	elseif nOpType == 1 then
		if not CheckConsumeCond( sysarg, consumes.upGradeGoldConsumes, 1 ) then
			return false
		end
		
		if not DoConsumeCond( sysarg, consumes.upGradeGoldConsumes, 1, 174, Lang.LogTips.l00095) then
			return
		end
	else
		return
	end
	
	Actor.setDiamond(sysarg, nOwnerType, nEquipPos, nSlotIdx, newItemId, nHeroPos)
	
end

--宝石熔炼
DiamondOpFuncList[DiamondOpId.enSmelt] = function (sysarg,args)
	local opType = args[2] --操作类型
	if opType == 0 then
		SendDiamondSmeltData(sysarg)
		return
	elseif opType == 1 then
	else
		return
	end
	local days = System.getDaysSinceOpenServer()
	local config = GetElemByCond(DiamondConfig.diamondSmelt, days)
	if not config then
		return
	end

	if not CheckConsumeCond( sysarg, config.diamondSmeltConsume, 1 ) then
		return
	end

	local index, itemSmeltList = GetItemIdxRandExt(config.diamondSmeltList)
	if not itemSmeltList then
		return
	end
	local awardList = itemSmeltList.awards
	if not awardList then
		return
	end
    local idx, item = GetItemIdxRandExt(awardList)
    if not item then
    	return
    end

    if not IsBagGridEnough(sysarg, {item}) then
    	Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0015, ttFlyTip)
		return 
	end

    if not DoConsumeCond( sysarg, config.diamondSmeltConsume, 1, LogIds.DiamondSmeltLogId, Lang.LogTips.DiamondSmelt) then
		return
	end

	if item.broadcast and item.broadcast ~= "" then
		local broadcast = item.broadcast
    	System.broadcastTipmsg(string.format(broadcast, Actor.getName(sysarg), Item.getItemLinkMsg(item.id)),ttScreenCenter + ttChatWindow)

	    local saveData = {}
	    saveData.param = Actor.getName(sysarg)
	    saveData.param1 = item.type
	    saveData.param2 = item.id
	    saveData.param3 = item.count
	    setRecordGlobalTableData(RecordTableIndexDefine.DiamondSmelt, saveData, DiamondConfig.keepRecordCount)
	end

	GiveCommonAward(sysarg, {item}, LogIds.DiamondSmeltLogId, Lang.LogTips.DiamondSmelt)

	SendDiamondSmeltData(sysarg, item.id)
end

function SendDiamondSmeltData(sysarg, itemId)
	local npack = DataPack.allocPacket(sysarg, 43, 8)
	if not npack then
		return 
	end
	DataPack.writeShort(npack, itemId or 0)

    local dVar = Actor.getDyanmicVar(sysarg)
    local circle = dVar.record_diamond_smelt_circle or 1
    local index = dVar.record_diamond_smelt_index or 1

    local dataTable, curCicle, curIndex = getRecordGlobalTableData(RecordTableIndexDefine.DiamondSmelt, circle, index, DiamondConfig.keepRecordCount)
    dVar.record_diamond_smelt_circle =  curCicle
    dVar.record_diamond_smelt_index =  curIndex
    if dataTable then
        local len = #dataTable
        --print("len ="..len)
        DataPack.writeByte(npack, len)
        for idx = 1, len do
            --print("idx = "..idx)
            DataPack.writeString(npack, dataTable[idx].param)
            DataPack.writeByte(npack, dataTable[idx].param1)
            DataPack.writeShort(npack, dataTable[idx].param2)
            DataPack.writeInt(npack, dataTable[idx].param3)
        end
    else
        --print("len = 0")
        DataPack.writeByte(npack, 0)
    end

    DataPack.flush(npack) 
end

--宝石分解
DiamondOpFuncList[DiamondOpId.enDestroy] = function (sysarg,args)
	local itemPtr = args[2]	--所属实体类型
	if not itemPtr then
		return
	end
	local itemId = Item.getItemId(itemPtr)
	local itemCount = Item.getItemProperty(sysarg, itemPtr, Item.ipItemCount, 0)
	local itemType = Item.getItemPropertyById(itemId, Item.ipItemType)
	if itemType ~= Item.itItemDiamond then
		return
	end

	if itemCount <= 0 then
		return
	end

	local itemIndex = Item.getItemPropertyById(itemId,Item.ipItemStaticDuaMax)

	local itemLevel = Item.getItemPropertyById(itemId,Item.ipItemSuitId)

	if not DiamondConfig.diamondDestroy or not DiamondConfig.diamondDestroy[itemIndex] or not DiamondConfig.diamondDestroy[itemIndex][itemLevel] then
		return
	end

	local awards = DiamondConfig.diamondDestroy[itemIndex][itemLevel]

	awards = GetAwardsByRate( awards, itemCount )

	local useSucc = Awards.Give(sysarg, awards, LogIds.DiamondDestroyLogId, Lang.LogTips.DiamondDestroy)
	if useSucc then 
		Actor.removeItemByPtr(sysarg, itemPtr, itemCount, true, Lang.LogTips.DiamondDestroy, LogIds.DiamondDestroyLogId)
		return true
	end
	
end

--一键分解
DiamondOpFuncList[DiamondOpId.enOneKey] = function (sysarg,args)
	local nDestroyLevel = args[2]	--所属实体类型
	local itemPtrList = LuaHelp.getBagStoneListByStoneLevel(sysarg, nDestroyLevel)
	if not itemPtrList then
		return
	end
	local myItemTable = {}
	for idx, itemPtr in ipairs(itemPtrList) do
		local itemId = Item.getItemId(itemPtr)
		local itemCount = Item.getItemProperty(sysarg, itemPtr, Item.ipItemCount, 0)
		if not myItemTable[itemId] then
			myItemTable[itemId] = 0
		end
		--print("itemId ="..itemId)
		--print("itemCount ="..itemCount)
		myItemTable[itemId] = myItemTable[itemId] + itemCount
	end
	local consumes = {}
	local awards = {}
	for itemId, itemCount in pairs(myItemTable) do
		--print("itemId ="..itemId)
		--print("itemCount ="..itemCount)
		local consume = {type = 0, id = itemId, count = itemCount}
		table.insert(consumes, consume)
		local itemIndex = Item.getItemPropertyById(itemId,Item.ipItemStaticDuaMax)
		--print("itemIndex ="..itemIndex)
		local itemLevel = Item.getItemPropertyById(itemId,Item.ipItemSuitId)
		--print("itemLevel ="..itemLevel)
		if not DiamondConfig.diamondDestroy or not DiamondConfig.diamondDestroy[itemIndex] or not DiamondConfig.diamondDestroy[itemIndex][itemLevel] then
			return
		end
		local award = DiamondConfig.diamondDestroy[itemIndex][itemLevel]
		award = GetAwardsByRate( award, itemCount )
		awards = EasyMergeAward(awards,award)
	end

	if consumes == {} or awards == {} then
		return
	end

	if not CheckConsumeCond( sysarg, consumes, 1 ) then
		return
	end

	if not IsBagGridEnough(sysarg, awards) then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0015, ttFlyTip)
		return 
	end
	
	if not DoConsumeCond( sysarg, consumes, 1, LogIds.DiamondDestroyLogId, Lang.LogTips.DiamondDestroy) then
		return
	end

	GiveCommonAward(sysarg, awards, LogIds.DiamondDestroyLogId, Lang.LogTips.DiamondDestroy)

end

EventCallDispatcher.registerEventCall(BaseTypes.ActorEventId.aeDiamondOp, EventCallDispatcher.HandlerDiamondOp)